package com.youdianstar.app.ui

import android.widget.CompoundButton
import com.youdianstar.app.R
import com.youdianstar.app.BR
import com.youdianstar.app.databinding.ActivityReportLossBinding
import com.youdianstar.app.entity.OrderInfo
import com.youdianstar.app.support.paymentActionIntent
import com.youdianstar.app.util.DataAppManager
import com.youdianstar.app.ui.web.WebActivity

import com.youdianstar.app.common.BisType
import com.youdianstar.app.common.ConfigKey
import com.youdianstar.app.common.Extra
import com.youdianstar.app.common.HTTP_BRS_URL
import com.youdianstar.app.common.Language
import com.youdianstar.app.common.base.BaseActivity
import com.youdianstar.app.common.ex.setThrottleListener
import com.youdianstar.app.common.ex.getObject
import com.youdianstar.app.common.ex.setAgreementLine
import com.youdianstar.app.common.util.LanguageConfig
import com.youdianstar.app.common.util.MathUtil

import org.koin.androidx.viewmodel.ext.android.viewModel
import java.util.Locale
import com.youdianstar.app.common.auko.startActivity

/**
 * File: ReportLossActivity
 * Author: 82149 Create: 2021/11/18 19:34
 * Changes (from 2021/11/18)
 * --------------------------------------------------
 * description: 报失
 * ---------------------------------------------------
 *
 */
class ReportLossActivity :BaseActivity<ActivityReportLossBinding, ReportViewModel>(){

    override fun layoutId(): Int = R.layout.activity_report_loss

    override fun initVariableId(): Int = BR.viewModel

    override fun initViewModel(): ReportViewModel {
        val model: ReportViewModel by viewModel()
        return model
    }

    private var orderNo : String ? =null
    private var penaltyAmount = 0.0F
    private var orderJson : OrderInfo? =null

    override fun initData() {
        super.initData()
        mBinding.headBar.initTitle(this, getString(R.string.home_reportLost), showInner = true)

        orderNo = intent.getStringExtra(Extra.IT_ORDERNO)
        penaltyAmount = intent.getFloatExtra(Extra.IT_MONEY,0F)
        orderJson = intent.getObject(Extra.IT_DATA)

        val symbolStr = DataAppManager.instance.symbolTx()
        mBinding.tvLossContent.text = String.format(getString(R.string.home_lostText),symbolStr+MathUtil.round(penaltyAmount))

        /*用户协议*/
        setAgreementLine(mBinding.tvAgreeContent,part1Tx = getString(R.string.login_agreeTo)+" ",
            String.format(getString(R.string.login_TxContent),getString(R.string.login_termOfUse)),"")
        mBinding.boxDefault.isChecked = true
        mBinding.boxDefault.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener {
            override fun onCheckedChanged(button: CompoundButton?, isCheck: Boolean) {
                mBinding.btnConfirm.isEnabled = isCheck
            }
        })
        mBinding.btnConfirm.isEnabled = true
    }

    override fun initClickEvent() {
        mBinding.btnConfirm.setThrottleListener {
            //跳转 支付页面
            paymentActionIntent(this@ReportLossActivity, BisType.Bis_Loss, amount = penaltyAmount.toDouble(), orderNo = orderNo , orderInfo = orderJson)
        }

        mBinding.tvAgreeContent.setThrottleListener {
            val langParam = getAgreementLangParam()
            val termsUrl = "${HTTP_BRS_URL}/sw/app/agreement/user.html?lang=$langParam"
            startActivity<WebActivity>(
                Extra.IT_DATA to termsUrl,
                Extra.IT_NOTIFY to getString(R.string.login_termOfUse)
            )
        }
    }

    /**
     * 获取协议语言参数
     * 规则：中文(zh) -> cn，其他语言 -> vi（默认）
     */
    private fun getAgreementLangParam(): String {
        val languageCode = LanguageConfig.instance.getLanguageCode()
        return if (languageCode == Language.ZH) "cn" else "vi"
    }
}